package com.example.gokuplayalong.service.fragments.main;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import java.util.ArrayList;
import java.util.List;

public class UserMainViewPagerAdapter extends FragmentStateAdapter {
    List<Fragment> fragments = new ArrayList<>();

    FragmentManager fragmentManager;

    public UserMainViewPagerAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle, List<Fragment> fragments) {
        super(fragmentManager, lifecycle);
        this.fragments = fragments;
        this.fragmentManager = fragmentManager;
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        return fragments.get(position);
    }

    @Override
    public int getItemCount() {
        return fragments.size();
    }

    public void setMaxLifeCycle(int position, Lifecycle.State state) {
        if (fragments.get(position).isAdded()) {
            fragmentManager.beginTransaction().setMaxLifecycle(fragments.get(position), state).commitNow();
        }
    }

    public Fragment getItem(int position) {
        return fragments.get(position);
    }

}
